//
//  DoubleRecordPrepareVC.m
//  record_plugin
//
//  Created by zqj on 2024/9/4.
//

#import "DoubleRecordPrepareVC.h"
#import "DoubleRecordVC.h"
#import "Constant.h"
#import "NetworkingTool.h"
@interface DoubleRecordPrepareVC ()<AVDRoomDelegate>
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (strong, nonatomic) NSTimer *timer;
@property (assign, nonatomic) NSInteger time;
@property (nonatomic, strong)AVDRoom *mRoom;/**房间模块*/
@property (strong, nonatomic) NSString * roomID;

@end

@implementation DoubleRecordPrepareVC

- (void)dealloc{
    NSLog(@"DoubleRecordPrepareVC ==== dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"人工视频";
//    WeakSelf;
//    [[NetworkingTool sharedInstance] getAuthTokenWithServerPath:baseUrl handler:^(NSString * _Nonnull accessToken) {
//        [weakSelf setupTimer];
//    } failure:^(NSError * _Nonnull error) {
//            
//    }];
    
    [self setupTimer];
   
}
- (void)joinRoom:(NSString *)roomid{
    self.roomID = roomid;
    self.mRoom = [AVDRoom obtain:roomid];
    [self.mRoom setOption:ro_room_rejoin_times value:@"-1"];
    AVDUser *user = [[AVDUser alloc]initWithUserId:[NSUUID UUID].UUIDString userName:@"tset" userData:@""];
    [self.mRoom joinWithUser:user delegate:self];
}

#pragma mark roomDelegate
- (void)onJoinResult:(AVDResult)result{
    NSString *msg = @"";
    if(result == AVD_Success){
        msg = @"加入成功！！！！";
        [self go2RecordVC];
    }else{
        msg = [NSString stringWithFormat:@"加入房间失败 code = %ld",(long)result];
    }
    NSLog(msg);
}

- (void)go2RecordVC{
    [self destoryTimer];
    NSBundle *SDKBundle = [NSBundle bundleForClass:[DoubleRecordVC class]];
    DoubleRecordVC * vc = [[DoubleRecordVC alloc] initWithNibName:@"DoubleRecordVC" bundle:SDKBundle];
    vc.roomID = self.roomID;
    vc.checkScore = self.faceCheckScore;
    vc.applyId = self.applyId;
    vc.nickName = self.customerName;
    vc.phone = self.phone;
    vc.productId = [NSString stringWithFormat:@"%ld",self.productId];
    vc.customerId = [NSString stringWithFormat:@"%ld",[NetworkingTool sharedInstance].customerId];
   [self.navigationController pushViewController:vc animated:YES];
}

- (void)setupTimer{
    WeakSelf;
    self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (weakSelf.time % 3 == 0) {
            [weakSelf joinQueue];
        }
        weakSelf.time += 1;
        [weakSelf formatSeconds:self.time];
    }];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}

- (void )formatSeconds:(NSInteger)totalSeconds {
    NSInteger minutes = totalSeconds / 60;
    NSInteger seconds = totalSeconds % 60;
    NSString *time = [NSString stringWithFormat:@"%02ld:%02ld", minutes, seconds];
    self.timeLab.text = time;
}

- (void)destoryTimer{
    [[NetworkingTool sharedInstance] cancleReq];
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }

}

- (IBAction)hangupClicked:(UIButton *)sender {
    [self destoryTimer];
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark httpReq
- (void)joinQueue{
    WeakSelf;
    [[NetworkingTool sharedInstance] joinQueue:self.productId name:self.customerName handler:^(NSString * _Nonnull roomId) {
        [weakSelf destoryTimer];
        [weakSelf joinRoom:roomId];
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"加入排队失败");
    }];
}

@end
